package com.easymin.daijia.server.ali;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ybkj.daijia.serverUtils.IOUtils;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.client.RestTemplate;

/**
 * @author zhyu
 * @date 2019/12/24 15:01
 * 银行卡识别, 银行名和卡号都出来了的
 * 立体的话识别错误率高, 识别错了你也不知道的, 顶天了看看位数, 所以一定要当事人确认.
 */
public class BankCard {
    public static void main(String[] args) {
        String host = "https://yhk.market.alicloudapi.com";
        String path = "/rest/160601/ocr/ocr_bank_card.json";
        RestTemplate template = new RestTemplate();
        String method = "POST";
        String appcode = Constant.APPCODE;
        Map<String, String> headers = new HashMap();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap();
        String str = IOUtils.readFileToBase64(Constant.BASE_PATH + "bankCard1e.jpg");
//        String str = IOUtils.readFileToBase64(Constant.BASE_PATH + "driver1Idcard.png");
        if (str != null) {
            String bodys = "{\"image\":\"" +  str + "\"}";
            //HTTP/1.1 463 Unknown  HTTP/1.1 200 OK
            try {
                HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
                if(200 == response.getStatusLine().getStatusCode()) {
                    InputStream content = response.getEntity().getContent();
                    byte[] bytes = new byte[content.available()];
                    content.read(bytes);
                    JSONObject jsonObject = JSON.parseObject(new String(bytes));
                    System.out.println(jsonObject.getString("bank_name"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
